Ubuntu 16.04
Sponsored Link

Perl + mod_perl
2016/06/13
 
Install mod_perl to make Perl scripts be fast.
[1] Install mod_perl.
root@www:~#
apt-get -y install libapache2-mod-perl2
[2] Configure PerlRun mode which always put Perl interpreter on RAM.
root@www:~#
vi /etc/apache2/conf-available/mod_perl.conf
# create new

# for example, set PerlRun mode under the "/var/www/perl"

PerlSwitches -w
PerlSwitches -T

Alias /perl /var/www/perl
<Directory /var/www/perl>
    AddHandler perl-script .cgi .pl
    PerlResponseHandler ModPerl::PerlRun
    PerlOptions +ParseHeaders
    Options +ExecCGI
</Directory>

<Location /perl-status>
    SetHandler perl-script
    PerlResponseHandler Apache2::Status
    Require ip 127.0.0.1 10.0.0.0/24
</Location>

root@www:~#
a2enconf mod_perl

Enabling conf mod_perl.
To activate the new configuration, you need to run:
  service apache2 reload
root@www:~#
systemctl restart apache2
[3] Create a test Script to make sure the settings are no ploblem. It's OK if the result like follows is displayed.
root@www:~#
mkdir /var/www/perl

root@www:~#
vi /var/www/perl/test-mod_perl.cgi
#!/usr/bin/perl

use strict;
use warnings;

print "Content-type: text/html\n\n";
print "<html>\n<body>\n";
print "<div style=\"width:100%; font-size:40px; font-weight:bold; text-align:center;\">";

my $a = 0;
&number();

print "</div>\n</body>\n</html>";

sub number {
    $a++;
    print "number \$a = $a";
}

root@www:~#
chmod 705 /var/www/perl/test-mod_perl.cgi

[4] Configure Registry mode which has caches of codes on RAM.
root@www:~#
vi /etc/apache2/conf-enabled/mod_perl.conf
Alias /perl /var/www/perl
<Directory /var/www/perl>
    AddHandler perl-script .cgi .pl
    # comment out PerlRun mode and add Registry mode like follows
    #PerlResponseHandler ModPerl::PerlRun
    PerlResponseHandler ModPerl::Registry
    PerlOptions +ParseHeaders
    Options +ExecCGI
</Directory>

root@www:~#
systemctl restart apache2
[5] Access to the test script which is an example of [4] section, then variable increases by reloading because variable is cached on RAM. So it's necessarry to edit the code for Registry mode.
root@www:~#
vi /var/www/perl/test-mod_perl.cgi
#!/usr/bin/perl

use strict;
use warnings;

print "Content-type: text/html\n\n";
print "<html>\n<body>\n";
print "<div style=\"width:100%; font-size:40px; font-weight:bold; text-align:center;\">";

my $a = 0;
&number(
$a
);
print "</div>\n</body>\n</html>";

sub number {
    my($a) = @_;

    $a++;
    print "number \$a = $a";
}
[6] By the way, it's possible to see the status of mod_perl to access to "http://(hostname or IP address)/perl-status".
 
Tweet